import QtQuick
import QtQuick.Layouts

import Fk
import Fk.Components.Common
import Fk.Components.GameCommon as Game

Rectangle {
  id: root
  color: "#AAFFFFFF"
  width: 200
  height: 200
  radius: 6

  property int playerid: 0
  property string avatar: ""
  property string screenName: ""
  property string netState: playerid > 0 ? "在线" : "人机"
  property string role: ""
  property bool thinking: false
  property int duration
  property bool surrendered: false

  Rectangle {
    color: "white"
    opacity: 0.7
    visible: root.surrendered
    anchors.fill: parent
    radius: 16
    z: 2
  }

  ColumnLayout {
    anchors.fill: parent
    // spacing: 4
    Text {
      id: playerName
      Layout.fillWidth: true
      Layout.alignment: Qt.AlignHCenter

      font.pixelSize: 16
      text: {
        let ret = root.screenName;
        if (Config.blockedUsers?.includes(screenName))
        ret = Lua.tr("<Blocked> ") + ret;
        return ret;
      }
      elide: root.playerid === Self.id ? Text.ElideNone : Text.ElideMiddle
      horizontalAlignment: Qt.AlignHCenter
    }

    Avatar {
      id: avatar
      Layout.alignment: Qt.AlignHCenter
      Layout.preferredWidth: 64
      Layout.preferredHeight: 64

      general: root.avatar
    }

    Text {
      id: netstateText
      Layout.alignment: Qt.AlignHCenter

      text: "状态：" + root.netState
      font.pixelSize: 16
    }

    Text {
      id: roleText
      Layout.alignment: Qt.AlignHCenter

      text: root.role
      font.pixelSize: 18
    }

    Text {
      id: thinkingText
      Layout.alignment: Qt.AlignHCenter

      text: root.thinking ? `思考中... 还剩${root.duration}秒` : "等待对手落子"
      font.pixelSize: 18
    }

    Item {}
  }

  Timer {
    id: countdownTimer
    interval: 1000
    repeat: true
    running: root.thinking
    onTriggered: {
      if (root.duration > 0) {
        root.duration--;
      } else {
        root.thinking = false;
      }
    }
  }

  Image {
    id: surrenderText
    source: Cpp.path + "/packages/chess-games/image/nchess/surrendered.svg"
    sourceSize: Qt.size(300, 120)
    visible: root.surrendered
    width: parent.width
    anchors.verticalCenter: parent.verticalCenter
    z:3
  }

  Game.ChatBubble {
    id: chat
    width: 175
    anchors.horizontalCenter: parent.horizontalCenter
    y: -16
  }

  function chat(msg) {
    chat.text = msg;
    chat.visible = true;
    chat.show();
  }

}
